//
//  RegisterViewController.m
//  ChineseCollegeStudentsNetwork
//
//  Created by 刘喆 on 2018/5/10.
//  Copyright © 2018年 刘喆. All rights reserved.
//

#import "RegisterViewController.h"
#import "CEAccessoryTextField.h"
#import "SJNetworkHelper.h"
#import "AppDelegate.h"
#import "UserCenterViewController.h"
#import "RegisterRequest.h"

@interface RegisterViewController ()<UITextFieldDelegate>

/** 标题 */
@property(nonatomic, strong) UILabel *titleLabel;
/** 用户名 */
@property(nonatomic, strong) CEAccessoryTextField *usernameTextField;
/** 姓名 */
@property(nonatomic, strong) CEAccessoryTextField *nameTextField;
/** 邮箱 */
@property(nonatomic, strong) CEAccessoryTextField *emailTextField;
/** 手机号 */
@property(nonatomic, strong) CEAccessoryTextField *phonenumberTextField;
/** 密码 */
@property(nonatomic, strong) CEAccessoryTextField *passwordTextField;
/** 登录按钮 */
@property(nonatomic, strong) UIButton *sureButton;

@end

@implementation RegisterViewController

#pragma mark - Life Cycle

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"注册";
    self.view.backgroundColor = [UIColor whiteColor];
    [self setupUI];
    [self setupConstraints];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated {
    
    [super viewWillDisappear:animated];
    [self resignAllTextFieldResponder];
}

#pragma mark = Network

- (void)setupUI {
    [self setupLeftWithTitle:@"" actionBlock:^{
        
    }];
    WEAK_SELF
    [self setupRightWithTitle:@"取消" actionBlock:^{
        STRONG_SELF
        [self.navigationController popViewControllerAnimated:YES];
    }];
}

#pragma mark - Constraints

- (void)setupConstraints {
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(60);
        make.centerX.equalTo(self.view);
        make.height.mas_equalTo(@50);
    }];
    
    [self.usernameTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.titleLabel.mas_bottom).offset(50);
        make.centerX.equalTo(self.view);
        make.left.equalTo(self.view).offset(0);
        make.right.equalTo(self.view).offset(0);
        make.height.mas_equalTo(@50);
    }];
    
    [self.nameTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.usernameTextField.mas_bottom).offset(10);
        make.centerX.equalTo(self.view);
        make.left.equalTo(self.usernameTextField);
        make.right.equalTo(self.usernameTextField);
        make.height.mas_equalTo(@50);
    }];
    
    [self.emailTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.nameTextField.mas_bottom).offset(10);
        make.centerX.equalTo(self.view);
        make.left.equalTo(self.usernameTextField);
        make.right.equalTo(self.usernameTextField);
        make.height.mas_equalTo(@50);
    }];
    
    [self.phonenumberTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.emailTextField.mas_bottom).offset(10);
        make.centerX.equalTo(self.view);
        make.left.equalTo(self.usernameTextField);
        make.right.equalTo(self.usernameTextField);
        make.height.mas_equalTo(@50);
    }];
    
    [self.passwordTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.phonenumberTextField.mas_bottom).offset(10);
        make.centerX.equalTo(self.view);
        make.left.equalTo(self.usernameTextField);
        make.right.equalTo(self.usernameTextField);
        make.height.mas_equalTo(@50);
    }];
    
    [self.sureButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.passwordTextField.mas_bottom).offset(70);
        make.left.mas_equalTo(20);
        make.right.mas_equalTo(-20);
        make.height.mas_equalTo(@50);
    }];
}

#pragma mark - Lazy Loading

-  (UILabel *)titleLabel {
    
    if (!_titleLabel) {
        
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.font = [UIFont systemFontOfSize:25];
        _titleLabel.textAlignment = NSTextAlignmentLeft;
        _titleLabel.textColor = [UIColor blackColor];
        _titleLabel.text = @"用户注册";
        
        [self.view addSubview:_titleLabel];
    }
    return _titleLabel;
}

- (CEAccessoryTextField *)usernameTextField {
    
    if (!_usernameTextField) {
        
        _usernameTextField = [[CEAccessoryTextField alloc] init];
        _usernameTextField.type = CEAccessoryTextFieldTypeLabel;
        _usernameTextField.titleLabel.text = @"用户名";
        _usernameTextField.delegate = self;
        
        [self.view addSubview:_usernameTextField];
    }
    return _usernameTextField;
}

- (CEAccessoryTextField *)nameTextField {
    
    if (!_nameTextField) {
        
        _nameTextField = [[CEAccessoryTextField alloc] init];
        _nameTextField.type = CEAccessoryTextFieldTypeLabel;
        _nameTextField.titleLabel.text = @"姓名";
        _nameTextField.delegate = self;
        
        [self.view addSubview:_nameTextField];
    }
    return _nameTextField;
}

- (CEAccessoryTextField *)emailTextField {
    
    if (!_emailTextField) {
        
        _emailTextField = [[CEAccessoryTextField alloc] init];
        _emailTextField.type = CEAccessoryTextFieldTypeLabel;
        _emailTextField.titleLabel.text = @"邮箱";
        _emailTextField.delegate = self;
        
        [self.view addSubview:_emailTextField];
    }
    return _emailTextField;
}

- (CEAccessoryTextField *)phonenumberTextField {
    
    if (!_phonenumberTextField) {
        
        _phonenumberTextField = [[CEAccessoryTextField alloc] init];
        _phonenumberTextField.type = CEAccessoryTextFieldTypeLabel;
        _phonenumberTextField.titleLabel.text = @"手机号";
        _phonenumberTextField.delegate = self;
        
        [self.view addSubview:_phonenumberTextField];
    }
    return _phonenumberTextField;
}

- (CEAccessoryTextField *)passwordTextField {
    
    if (!_passwordTextField) {
        
        _passwordTextField = [[CEAccessoryTextField alloc] init];
        _passwordTextField.type = CEAccessoryTextFieldTypeLabel;
        _passwordTextField.titleLabel.text = @"密码";
        _passwordTextField.secureTextEntry = YES;
        _passwordTextField.delegate = self;
        
        [self.view addSubview:_passwordTextField];
    }
    return _passwordTextField;
}

- (UIButton *)sureButton {
    
    if (!_sureButton) {
        
        _sureButton = [[UIButton alloc] init];
        _sureButton.backgroundColor = SMCTHEME;
        _sureButton.layer.cornerRadius = 5.0f;
        _sureButton.layer.masksToBounds = YES;
        
        [_sureButton setTitle:@"确定" forState:UIControlStateNormal];
        [_sureButton setTitle:@"确定" forState:UIControlStateHighlighted];
        
        @weakify(self);
        [[_sureButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
            @strongify(self);
            
            
            if([self.usernameTextField.text isEqualToString:@""] || !self.usernameTextField.text) {
                ShowMessage(@"请输入用户名");
            }else if([self.nameTextField.text isEqualToString:@""] || !self.nameTextField.text) {
                ShowMessage(@"请输入姓名");
            }else if([self.emailTextField.text isEqualToString:@""] || !self.emailTextField.text) {
                ShowMessage(@"请输入邮箱");
            }else if([self.phonenumberTextField.text isEqualToString:@""] || !self.phonenumberTextField.text) {
                ShowMessage(@"请输入手机号");
            }else if([self.passwordTextField.text isEqualToString:@""] || !self.passwordTextField.text) {
                ShowMessage(@"请输入密码");
            }else {
                
                NSDictionary *params = @{@"userName":self.usernameTextField.text,
                                         @"displayName":self.nameTextField.text,
                                         @"password":self.passwordTextField.text,
                                         @"mobile":self.phonenumberTextField.text,
                                         @"email":self.emailTextField.text};

                [MLAHttpManage postHttpType:MLAHttpRequestType_Post hostType:MLAHttpHostType_Api api:@"/api/v1/users" params:params.mj_JSONString success:^(id  _Nonnull response, NSString * _Nonnull msg) {
                    ShowMessage(@"注册成功");
                    [self.navigationController popViewControllerAnimated:YES];
                } failure:^{
                    ShowMessage(@"注册失败");
                }];
                
            }
        }];
        
        [self.view addSubview:_sureButton];
    }
    return _sureButton;
}
-(void)dismiss {
    dispatch_async(dispatch_get_main_queue(), ^{
        AppDelegate *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
        delegate.tabBarController.selectedIndex = 3;
        delegate.window.rootViewController = delegate.tabBarController;
        if ([delegate.window.rootViewController isKindOfClass:[BaseTabBarController class]]) {
            [[BaseViewController topViewController].navigationController dismissViewControllerAnimated:YES completion:nil];
            [[BaseViewController topViewController] dismissViewControllerAnimated:YES completion:nil];
            
        }
    });
}
#pragma mark - UITextFieldDelegate

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    
    [textField resignFirstResponder];
    
    return YES;
}

#pragma mark - UIControl

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self resignAllTextFieldResponder];
}

#pragma mark - Private

- (void)resignAllTextFieldResponder {
    
    [self.usernameTextField resignFirstResponder];
    [self.nameTextField resignFirstResponder];
    [self.emailTextField resignFirstResponder];
    [self.phonenumberTextField resignFirstResponder];
    [self.passwordTextField resignFirstResponder];
}


@end
